/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.explorer;

import org.json.JSONObject;
import org.webos.java.base.Application;
import org.webos.java.base.ApplicationType;
import org.webos.java.base.DBFileProvider;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.Session;
import org.webos.java.base.fs.FileDescriptor;
import org.webos.java.base.fs.FileManager;
import org.webos.java.base.workspace.FrameHolder;
import org.webos.java.base.workspace.Splitter;
import org.webos.java.base.workspace.Workspace;
import org.webos.java.explorer.ftypes.TextFileType;
import org.webos.java.root.Log;
import org.webos.java.root.RandomGUID;
import org.webos.java.base.Frame;
import org.webos.java.base.fs.FileType;
import org.webos.java.explorer.ftypes.ApplicationFileType;

/**
 *
 * @author Kostya
 */
@ApplicationType(code="explorer", jsClass="explorer.Explorer", 
    name="File browser", version="0.1", systemType=true, 
    icon="explorer_icn explorer_icn_icon", errorCode=10)
public class Explorer extends Application{

    public Explorer(Session aSession) {
        super(aSession);
        createNewWindow(true);
    }

    static {
        TextFileType textFileType = new TextFileType();
        FileManager.setFileType(textFileType);
        ApplicationFileType applicationFileType = new ApplicationFileType();
        FileManager.setFileType(applicationFileType);
    }

    private void createNewWindow(boolean first){
        Workspace rootWorkspace = new Workspace(this, first? "explorer-root": new RandomGUID().toString());
        Splitter sp = new Splitter(null, "main-sp");
        sp.setVertical(true);
        sp.setRatio(0.4);
        FrameHolder fh0 = new FrameHolder(sp, "fh-tree");
        sp.setLeftItem(fh0);
        FrameHolder mainFrameHolder = new FrameHolder(sp, "fh-main");
        mainFrameHolder.setLocked(true);
        sp.setRightItem(mainFrameHolder);
        FileViewPanel viewPanel = new FileViewPanel(this, rootWorkspace);
        FileTreePanel2 treePanel = new FileTreePanel2(this, rootWorkspace, mainFrameHolder, viewPanel);
        rootWorkspace.setCloseable(false);
        if(!first){
            rootWorkspace.setCloseable(true);
            viewPanel.setCode(new RandomGUID().toString());
            treePanel.setCode(new RandomGUID().toString());
        }
        viewPanel.setTreePanel(treePanel);
        mainFrameHolder.addFrame(viewPanel);
        fh0.addFrame(treePanel);
        rootWorkspace.setCaption("File browser");
        rootWorkspace.setItem(sp);
        addWorkspace(rootWorkspace);
        addFrame(treePanel);
        addFrame(viewPanel);
        showWorkspace(rootWorkspace);
    }
//
//    private Workspace generateNewWorkspace(){
//        Workspace ws = new Workspace(this, "explorer-root");
//        FrameHolder fh0 = new FrameHolder(null, "wh-root");
//        ws.setCaption("File browser");
//        ws.setCloseable(false);
//        ws.setItem(fh0);
//        return ws;
//    }

    public void fileUpdated(FileDescriptor aFile, boolean aNewFile){
        fileUpdated(aFile, aNewFile, false);
    }
    
    public void fileUpdated(FileDescriptor aFile, boolean aNewFile, boolean aFullReload){
        DBFileProvider.getIcon(aFile);
        for (int i = 0; i < getFramesCount(); i++) {
            Frame frame = getFrameAt(i);
            if(frame instanceof FileTreePanel2){
                FileTreePanel2 fileTreePanel = (FileTreePanel2) frame;
                fileTreePanel.fileUpdated(aFile, aNewFile, aFullReload);
            }
        }
    }

    @RemoteMessage
    public JSONObject newWindow(JSONObject aData){
        try {
            createNewWindow(false);
        } catch (Throwable t) {
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
        }
        return null;
    }

    @Override
    protected void requestCloseWorkspace(Workspace aWorkspace) {
        closeWorkspace(aWorkspace);
    }


}
